<template>
  <div class="announcement">
    <!-- 搜索区域 -->
    <el-form ref="dataForm" class="announcement_nav">
      <div style="margin-bottom:0px;height:0px" />
      <el-button v-if="button1" class="add" type="primary" size="medium" @click="creat">添加一条</el-button>
    </el-form>

    <el-table
      :data="noticeListDtos"
      style="width: 100%"
      :border="true"
      :resizable="true"
      :highlight-current-row="true"
    >
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">{{ scope.row.notice_id }}</div>
        </template>
      </el-table-column>
      <el-table-column label="公告类型" width="150" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.notice_type }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" width="210" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.notice_time }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="公告主题" width="300" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.notice_theme }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="公告内容" header-align="center">
        <template slot-scope="scope" align="left">
          <div slot="reference" class="name-wrapper">
            <span v-html="scope.row.notice_content" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220" align="center">
        <template slot-scope="scope">
          <el-button v-if="button2" size="medium" type="success" @click="updata(scope.row)">编辑</el-button>
          <el-button v-if="button3" size="medium" type="danger" @click="deletes(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 列表 -->
    <el-col :span="24" class="toolbar" style="text-align: center;">
      <el-pagination
        background
        :current-page="currentPage"
        :page-count="pageNum"
        :page-size="pagesize"
        style="margin-top:30px;margin-bottom:30px"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-col>
    <el-dialog :visible.sync="deleteDataFrom" width="30%">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left:50px;"
      >
        <span>确定删除这一条吗？</span>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDataFrom = false">取消</el-button>
        <el-button type="primary" @click="deleteData">确定删除</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from '@/utils/request'
import { Message, popconfirm } from 'element-ui'
import eAnnouncement from '../eAnnouncement'
import { getToken, getAccount, getButtonpermsStr } from '@/utils/auth'
export default {
  components: {
    eAnnouncement
  },
  data() {
    return {
      deleteDataFrom: false,
      noticeListDtos: [],
      currentPage: 1, // 初始页
      pagesize: 8, // 当前页面内的列表行数
      pageNum: 1, // 总数页数，
      total: 0,
      rules: {},
      button1: false,
      button2: false,
      button3: false,
      buttonpermsStr: getButtonpermsStr(),
      temp: {
        notice_id: 0,
        notice_type: '',
        notice_time: '',
        notice_theme: '',
        notice_state: 0,
        notice_content: ''
      }
    }
  },
  mounted() {
    if (this.buttonpermsStr !== undefined) {
      if (this.buttonpermsStr.indexOf(157) > -1) {
        this.button1 = true
      }
      if (this.buttonpermsStr.indexOf(158) > -1) {
        this.button2 = true
      }
      if (this.buttonpermsStr.indexOf(159) > -1) {
        this.button3 = true
      }
    }

    this.getAnnouncements()
  },
  methods: {
    // 添加
    creat() {
      this.temp = {
        notice_id: '',
        notice_type: '',
        notice_time: '',
        notice_theme: '',
        notice_state: 0,
        notice_content: ''
      }
      this.$router.push({ name: '编辑公告信息', params: { data: this.temp }})
    },
    // 当前页
    handleSizeChange: function(pagesize) {
      this.pagesize = pagesize
      this.getAnnouncements()
    },
    // 换页：更新列表数据
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
      this.getAnnouncements()
    },
    // 修改公告
    updata(row) {
      this.temp = Object.assign(row)
      this.$router.push({ name: '编辑公告信息', params: { data: this.temp }})
    },
    // 删除公告
    deletes(row) {
      this.deleteDataFrom = true
      this.temp = {
        notice_id: row.notice_id,
        notice_type: row.notice_type,
        notice_time: this.temp.notice_time,
        notice_theme: row.notice_theme,
        notice_state: 1,
        notice_content: row.notice_content
      }
    },
    // 确定删除
    deleteData() {
      return request({
        url: '/v3/notice/update',
        method: 'post',
        headers: {
          account_type: getAccount()
        },
        data: this.temp
      }).then(res => {
        if (res.tip === '编辑成功.') {
          Message({
            message: '删除',
            type: 'success',
            duration: 0.5 * 1000
          })
          this.deleteDataFrom = false
          this.$router.go(0) // 刷新页面
        }
      })
    },
    // 获取所有列表
    getAnnouncements() {
      return request({
        url: '/v3/notice/list',
        method: 'post',
        headers: {
          account_type: getAccount()
        },
        data: {
          current_page: this.currentPage,
          page_size: this.pagesize,
          community_id: 5
        }
      }).then(res => {
        this.currentPage = res.current_page
        this.pageNum = res.pages
        this.total = res.total_record
        this.noticeListDtos = []
        res.noticeListDtos.map((value, index) => {
          var d = new Date(value.notice_time)
          value.notice_time =
            d.getFullYear() +
            '-' +
            (d.getMonth() + 1) +
            '-' +
            d.getDate() +
            ' ' +
            d.getHours() +
            ':' +
            d.getMinutes() +
            ':' +
            d.getSeconds()
        })
        this.noticeListDtos = res.noticeListDtos
      })
    }
  }
}
</script>
<style lang="scss" >
.announcement {
  margin-top: 55px;
  overflow: hidden;
  .name-wrapper {
    img {
      width: 100px;
      height: 100px;
    }
  }
  .announcement_nav {
    margin: 10px;
  }
}
</style>
